#!/bin/bash

# 想要关闭的target
target=$1

# 配置文件
config_file=config/config.${target}.lua
if [ -z ${target} ]; then
    config_file=config/config.lua
fi

if [ ! -f ${config_file} ]; then
    echo "config file ${config_file} not exist"
    exit 1
fi

# pid文件
pid_path=./game.${target}.pid
if [ -z $target ]; then
    pid_path=./game.pid
fi

if [ ! -f ${pid_path} ]; then
    echo "no such file: ${pid_path}"
    exit 1
fi

process_id=`cat ${pid_path}`
if [ -z $process_id ]; then
    echo "kill error, no such process"
    exit 1
fi

# 发送命令让数据库进行存储
ip=`sed -n -r 's/debug_ip *= *"([0-9\.]*)"/\1/p' $config_file`
port=`sed -n -r 's/debug_port *= *([0-9]+)/\1/p' $config_file`
if [ -z ${ip} ] || [ -z ${port} ]; then
    echo "debug_ip or debug_port not found in config file: $config_file"
    exit 1
fi

curl --http0.9 --noproxy "*" "http://${ip}:${port}/call/.GAME_LOGGER/\"flush\""
curl --http0.9 --noproxy "*" "http://${ip}:${port}/call/.MONGO_POOL/\"flush\""

kill $process_id
echo "kill success, target: ${target}, process_id: ${process_id}"